<?php

class CreateHiveTableAction extends HiveAction {
	
	public function __construct($name, $cmd, $hiveClient) {		
		parent::__construct($name, $cmd, $hiveClient);
	}
	
	public function execute(ezcWorkflowExecution $execution) {
		$source	= CJSON::decode($execution->getVariable('source'));
		$dest	= CJSON::decode($execution->getVariable('dest'));
		
    	$newTable	= $dest['storage'];
    	$select		= $source['filter']['select'];
    	
    	$this->cmd = $this->concreteParams($this->cmd, array(
    		':newTable'	=>	$newTable,
    		':columnDefinition'	=>	$this->generateColumns($select),	
    		)
    	);
 		return parent::execute($execution);
    }
    
    private function generateColumns($select) {    	
    	$columnDefine = '';
    	foreach ($select as $columnName => $columnType) {
    		$columnDefine .= "$columnName $columnType,";
    	}
    	
    	return substr($columnDefine, 0, strlen($columnDefine) - 1);
    }
}
